class oscIOHandler {
  OscP5 oscIn, oscOut;
  NetAddress remoteLocation;
  OscMessage message;
  Mod[] listeners=new Mod[255];
  String name, remoteIP, myIP, myGlobalIP, IPDynamic;
  int recievePort, sendPort, il, recieveDynamic, sendDynamic;
  Boolean active;
  oscIOHandler(String tempIP, int tempSP, int tempRP, String tempName) {
    active=false;
    remoteIP=tempIP;
    name=tempName;
    recievePort=tempRP;
    sendPort=tempSP;
    il=0;
    oscOut = new OscP5(this, sendPort);
    oscIn = new OscP5(this, recievePort);
    remoteLocation = new NetAddress(remoteIP, sendPort);
  }
  void setSendPort(int tempP) {
    sendPort=tempP;
    setRemoteLocation();
  }
  void setSendIP(String tempIP) {
    remoteIP=tempIP;
    setRemoteLocation();
  }
  void setRemoteLocation() {
    remoteLocation = new NetAddress(remoteIP, sendPort);
    oscOut = new OscP5(this, sendPort);
  }
  void setRecievePort(int tempP) {
    recievePort=tempP;
    oscIn = new OscP5(this, recievePort);
  }
  void addListener(Mod temp) {
    Boolean already=false;
    int i=0;
    try {

      do {
        if (listeners[i].uid==temp.uid)already=true;
        i++;
      }
      while (i<il && !already);
    }
    catch(NullPointerException e) {
    }

    if (!already) {
      listeners[il]=temp;
      il++;
    }
  }
  void removeListener(Mod temp) {
    Boolean found=false;
    int i=0;
    try {
      do {
        if (listeners[i].uid==temp.uid) {
          found=true;
        }
        else {
          i++;
        }
      }

      while (i<il && !found);
    }
    catch(NullPointerException e) {
    }
    if (found) {
      do {
        listeners[i]=listeners[i+1];
        i++;
      }
      while (i+1<il);
      il--;
    }
  }
  void sendOSC(String temp) {
    String tokens[]=splitTokens(temp, " ");
    message = new OscMessage(tokens[0]);
    for (int i=1;i<tokens.length;i++) {
      message.add(int(tokens[i]));
    }
    oscOut.send(message, remoteLocation);
  }


  void oscEvent(OscMessage theOscMessage) {
    //println(theOscMessage);

    if (active && il>0) {
      String out=theOscMessage.addrPattern();
      for (int i=0;i<theOscMessage.arguments().length;i++) {
        out=out+" "+str(theOscMessage.get(i).intValue());
        //println(" "+i+": "+str(theOscMessage.get(i).intValue()));
      }
      for (int i = 0; i<il; i++) {
        
        listeners[i].message=out;
        listeners[i].go();
      }
    }
  }
}

